//
//  MediaLibraryAccess_02ViewController.m
//  MediaLibraryAccess_02
//
//  Created by 畑圭輔 on 10/02/01.
//  Copyright Keisuke Hata. All rights reserved.
//

#import "MediaLibraryAccess_02ViewController.h"

@implementation MediaLibraryAccess_02ViewController

// 曲情報を取得する
- (IBAction) getSongInfo {
	// 1曲目の情報を取得する
	MPMediaItemCollection *collection = [_songs objectAtIndex:0];
	MPMediaItem* song = collection.representativeItem;
	
	// 各種情報を取得する
	_songTitle.text = [song valueForProperty:MPMediaItemPropertyTitle];
	_songGenre.text = [song valueForProperty:MPMediaItemPropertyGenre];
	_songArtist.text = [song valueForProperty:MPMediaItemPropertyArtist];
	NSNumber *count = [song valueForProperty:MPMediaItemPropertyPlayCount];
	_songPlayCount.text = [NSString stringWithFormat:@"%d回" , [count unsignedIntValue]];
}

- (void) alert:(NSString *)title message:(NSString *)message
{
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil] autorelease];
	[alert show];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	// 曲のタイトルでグルーピングされたデータタイプが"Music"のクエリーを取得し、
	// コレクション情報を取得します。
	MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
	_songs = [[songsQuery collections] retain];
	NSAssert( _songs != nil , @"曲の取得に失敗しました" );
	isExecute = YES;
	
	int songCount = [_songs count];
	if( songCount == 0 )
	{
		[self alert:@"警告" message:@"曲が１曲も入っていないためサンプルを実行することはできません"];
		isExecute = NO;
		_songInfoButton.enabled = NO;
	}
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[_songs release];
    [super dealloc];
}

@end
